$(function() {
	layui.use(['table', 'util','laypage','layer'], function() {
		var table = layui.table;
		var util = layui.util;

		function find_userInfo() {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/sysUser/findMyUserVo",
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						$("#name").text(result.data.user.name);
						$("#account").text(result.data.user.account);
						var sex = ["保密", "男", "女"];
						$("#sex").text(sex[result.data.user.sex]);
						$("#birthday").text(util.toDateString(result.data.user.birthday, "yyyy-MM-dd"));
						var phone = "";
						if(result.data.user.phone != null) {
							phone = result.data.user.phone;
						}
						$("#phone").text(phone);
						var address = "";
						if(result.data.user.address != null) {
							address = result.data.user.address
						}
						$("#address").text(address);
						$("#createTime").text(util.toDateString(result.data.user.create_time, "yyyy-MM-dd HH:mm:ss"));
						$("#modifyTime").text(util.toDateString(result.data.user.update_time, "yyyy-MM-dd HH:mm:ss"));
					}

				}
			});

		}

		find_userInfo();
		
	  
		var laypage = layui.laypage;
		var layer=layui.layer;
		var total; //总条数
		//定义获取当前用户登录日志信息的函数
		function find_log_list(page, rows) {
			var index;
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/loginLog/selectPageMyLoginLog",
				async: false,
				data: {
					"queryParams.curr_page": page,
					"queryParams.page_size": rows
				},
				"beforeSend": function() {
					index = layer.load(3, {
						shade: [0.5, "black"]
					})
				},
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.close(index);
						build_log_list(result);
						total = result.data.total;
					} else {
						layer.msg("服务器错误，请联系管理员", {
							icon: 2,
							time: 1000
						});
					}
				},
				"error": function() {
					layer.msg("服务器错误，请联系管理员", {
						icon: 2,
						time: 1000
					});
				}
			});

		}

		//定义渲染登录日志的函数
		function build_log_list(result) {
			var logList = result.data.list;
			$(".table_tbody").empty();
			//循环遍历日志数组，在页面上显示
			$.each(logList, function(index, item) {
				var tr = $("<tr></tr>");
				var td1 = $("<td></td>").text(item.user_account);
				var td2 = $("<td></td>").text(item.request_ip);
				var td3 = $("<td></td>").text(util.toDateString(item.login_date, "yyyy-MM-dd HH:mm:ss"));
				var type = ["pc端", "手机端"]
				var td4 = $("<td></td>").text(type[item.type]);
				var state = ["登录失败", "登录成功"]
				var td5 = $("<td></td>").text(state[item.state]);
				var td6 = $("<td></td>").text(item.model_number);
				var imei = "";
				if(item.imei != null) {
					imei = item.imei
				}
				var td7 = $("<td></td>").text(imei);
				tr.append(td1).append(td2).append(td3)
					.append(td4).append(td5).append(td6).append(td7).appendTo(".table_tbody")

			});
		}
		find_log_list(1, 10);
		laypage.render({
			elem: "table_page", //绑定页面元素节点的id，只能是id
			count: total, //数据总数，从服务中读取的数据中
			layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
			jump: function(obj, first) { //当前分页执行成功后的回调函数，obj包含了当前分页的所有参数，比如：
				//obj.curr,当前页码，obj.limit,当前页所容纳的最大条数
				if(!first) {
					find_log_list(obj.curr, obj.limit);
				}

			}
		});
		
	/*	table.render({
			elem: '#loginLog',
			url: "http://localhost:8080/ssmService/loginLog/selectPageMyLoginLog",
			page: true, //开启分页
		    cols: [
				[ //表头
					{
						field: 'account',
						title: '账号',
						width: 80,
						sort: true
					}, {
						field: 'ip',
						title: 'ip地址',
						width: 80
					}, {
						field: 'login_date',
						title: '登陆时间',
						width: 80,
						sort: true
					}, {
						field: 'type',
						title: '登录类型',
						width: 80
					}, {
						field: 'state',
						title: '登陆状态',
						width: 177
					}, {
						field: 'model_number',
						title: '手机/浏览器',
						width: 80,
						sort: true
					}, {
						field: 'ime',
						title: 'ime号',
						width: 80
					}
				]
			]

		});*/
	});
})